#include <iostream>
#include "ViewerException.h"
#include "NetworkSnapshotProviderRemote.h"

NetworkSnapshotPtr NetworkSnapshotProviderRemote::getSnapshot(std::ostream& errs) {
  using namespace net;
  using namespace rapidxml;
  NetworkSnapshotPtr snapshot(new NetworkSnapshot());

  SenderXml sender;
  ExtXml requestXMLDoc;
  ExtXml responseXMLDoc;

  xml_node<> *requestXMLNode = requestXMLDoc.allocate_node(node_element, "request");
  requestXMLNode->append_attribute(requestXMLDoc.allocate_attribute("type", "known-nodes")); 
  requestXMLNode->append_attribute(requestXMLDoc.allocate_attribute("answer-port", "")); 
  try {  
    responseXMLDoc = sender.send(collectorAddr, NGU_VIEWER_PORT, requestXMLDoc);
  } catch(NetException& ex) {
    throw ViewerException(ex.what());
  }

  std::cout << "Recieved XML: " << std::endl;
  std::cout << responseXMLDoc << std::endl;

  XMLNetworkSnapshotParser::parse(responseXMLDoc, snapshot, errs);  
  return snapshot;
}
